package solution.tree;

import common.TreeNode;
import common.util.TreeNodeUtil;

/**
 * @author : wenchao.long
 * @date : Created in 2021/7/13 14:58 周二
 * @description : 左叶子之和
 */
public class SumOfLeftLeavesSolution {

    public int sumOfLeftLeaves(TreeNode root) {
        return root == null ? 0 : bfs(root, 0);
    }

    private int bfs(TreeNode root, int sum) {
        if (root == null) {
            return sum;
        }
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sum += root.left.val;
        }
        sum = bfs(root.left, sum);
        sum = bfs(root.right, sum);
        return sum;
    }

    public static void main(String[] args) {
        TreeNode node = TreeNodeUtil.create(3, 9, 20, null, null, 15, 7);
        int sum = new SumOfLeftLeavesSolution().sumOfLeftLeaves(node);
        System.out.println(sum);
    }

}
